package com.rfid.client;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.rfid.listener.ResetListener;
import com.rfid.listener.SubmitListener;
import com.rfid.service.RFIDSaveInfoService;
import com.teatrace.model.Product;

@SuppressWarnings("serial")
public class SettingWindow extends JDialog {
	
	private JButton submit = new JButton("确定");
	private JButton reset = new JButton("取消");
	private JTextField idText = new JTextField();
	private JTextField nameText = new JTextField();
	
	private void initCmp() {

		JPanel buttonPanel = new JPanel();
		JPanel panel = new JPanel();
		JPanel logPanel = new JPanel();
		
		JPanel leftPanel = new JPanel();
		JPanel rightPanel = new JPanel();
		JPanel buttomPanel = new JPanel();
		JPanel contextPanel = new JPanel(new GridLayout(2,2,0,20));
		panel.setLayout(new BorderLayout(2,1));
		buttonPanel.add(submit);
		buttonPanel.add(reset);
		
		JLabel numberLable = new JLabel("ID:",10);
		
		JLabel addressLable = new JLabel("NAME:");
		
		Properties p = new Properties();
		try {
			p.load(this.getClass().getClassLoader().getResourceAsStream("initAttribute.properties"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		Product product = new Product();
		product.setId(Integer.parseInt(p.getProperty("ID")));
		product.setName(p.getProperty("NAME"));
		RFIDSaveInfoService.getProduct();
		
		idText.setText(product.getId().toString());
		nameText.setText(product.getName());
		
		contextPanel.add(numberLable);
		contextPanel.add(idText);
		contextPanel.add(addressLable);
		contextPanel.add(nameText);
		
		logPanel.setLayout(new GridLayout(1,1));
		logPanel.add(contextPanel);
		panel.add(buttonPanel,BorderLayout.NORTH);
		panel.add(logPanel,BorderLayout.CENTER);

		panel.add(leftPanel,BorderLayout.WEST);
		panel.add(rightPanel,BorderLayout.EAST);
		panel.add(buttomPanel,BorderLayout.SOUTH);
		this.add(panel);
		
	}
	
	private void initCmpListeners() {
		ActionListener submitListener = new SubmitListener(this);
		submit.addActionListener(submitListener);
		ActionListener resetListener = new ResetListener(this);
		reset.addActionListener(resetListener);
	}
	
	public SettingWindow(JFrame frame,boolean flag) {
		super(frame, flag);
		this.setTitle("写入信息设置");
		this.setSize(400, 300);
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension screenSize = kit.getScreenSize();
		int screenWidth = (int) (screenSize.getWidth()/2);
		int screenHight = (int) (screenSize.getHeight()/2);
		int width = this.getWidth();
		int hight = this.getHeight();
		this.setLocation(screenWidth-width/2, screenHight-hight/2);
		
		this.initCmp();
		this.initCmpListeners();
	}
	public JTextField getIdText() {
		return idText;
	}
	public JTextField getNameText() {
		return nameText;
	}
}
